<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 分享有礼专题信息
 * Class Base
 * @author heyu
 */
class Share extends MY_Controller {
    /**
     * @var AdmSettingModel
     */
    public $mSetting = null;

    /**
     * 初始化
     * User constructor.
     */
    public function __construct() {
        parent::__construct();
        $this->loadModel('AdmSettingModel', 'mSetting');
    }

    /**
     * 首页
     */
    public function index(){
        $this->db->select('share_img,register_img,down_img,share_step,content');

        $tA = $this->mSetting->getOne($this->mSetting->tbShare);
        $tA['share_img'] = base_url($tA['share_img']);
        $tA['register_img'] = base_url($tA['register_img']);
        $tA['down_img'] = base_url($tA['down_img']);
        $tA['share_step'] = base_url($tA['share_step']);

        $data = array(
            'info' => $tA,
            'title' => '分享有礼'
        );
        $this->view('config/share', $data, 'validation,ueditor,cropit_master,webuploader');
    }

    /**
     * 保存数据
     */
    public function save(){
        $content = trim(post('content'));
        $share_img = trim(post('share_img'));
        $share_step = trim(post('share_step'));
        $register_img = trim(post('register_img'));
        $down_img = trim(post('down_img'));
        if(!$content){
            $this->ajaxReturn(0, '请输入规则');
        }
        if(!$share_img){
            $this->ajaxReturn(0, '请上传分享页广告');
        }
        if(!$share_step){
            $this->ajaxReturn(0, '请上传分享步骤广告');
        }
        if(!$register_img){
            $this->ajaxReturn(0, '请上传注册页广告');
        }
        if(!$down_img){
            $this->ajaxReturn(0, '请上传下载页广告');
        }
        //分享页广告
        $tA1 = explode('?', str_replace(base_url(), '', $share_img));
        if (!file_exists($tA1[0])) {

            $this->ajaxReturn(0, '分享页广告上传失败');
        }
        //分享步骤广告
        $tA4 = explode('?', str_replace(base_url(), '', $share_step));
        if (!file_exists($tA4[0])) {

            $this->ajaxReturn(0, '分享页广告上传失败');
        }
        //注册页广告
        $tA2 = explode('?', str_replace(base_url(), '', $register_img));
        if (!file_exists($tA2[0])) {

            $this->ajaxReturn(0, '注册页广告上传失败');
        }
        //下载页广告
        $tA3 = explode('?', str_replace(base_url(), '', $down_img));
        if (!file_exists($tA3[0])) {

            $this->ajaxReturn(0, '下载页广告上传失败');
        }

        $data = [
            'content' => $content,
            'share_img' => $tA1[0],
            'register_img' => $tA2[0],
            'down_img' => $tA3[0],
            'share_step' => $tA4[0],
        ];

        if($this->mSetting->update($this->mSetting->tbShare, $data)){
            $this->ajaxReturn(1, '保存成功', null, getUrl('config/share'));
        }
        $this->ajaxReturn(0, '保存失败');
    }

}
